package org.rsbot.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.rsbot.bot.Bot;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.MethodContext;

public class ScreenshotUtil
{
  private static final Logger log = Logger.getLogger(ScreenshotUtil.class.getName());
  private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss");

  public static void saveScreenshot(Bot paramBot, boolean paramBoolean) {
    String str = dateFormat.format(new Date()) + ".png";
    File localFile = new File(GlobalConfiguration.Paths.getScreenshotsDirectory());
    if ((localFile.isDirectory()) || (localFile.mkdirs()))
      saveScreenshot(paramBot, new File(localFile, str), "png", paramBoolean);
  }

  public static void saveScreenshot(Bot paramBot, File paramFile, String paramString, boolean paramBoolean)
  {
    try {
      BufferedImage localBufferedImage = takeScreenshot(paramBot, paramBoolean);

      ImageIO.write(localBufferedImage, paramString, paramFile);
      log.info("Screenshot saved to: " + paramFile.getPath());
    } catch (Exception localException) {
      log.log(Level.SEVERE, "Could not take screenshot.", localException);
    }
  }

  public static BufferedImage takeScreenshot(Bot paramBot, boolean paramBoolean) {
    BufferedImage localBufferedImage1 = paramBot.getImage();
    WritableRaster localWritableRaster = localBufferedImage1.copyData(null);

    BufferedImage localBufferedImage2 = new BufferedImage(localBufferedImage1.getColorModel(), localWritableRaster, localBufferedImage1.isAlphaPremultiplied(), null);
    Graphics2D localGraphics2D = localBufferedImage2.createGraphics();

    if (paramBoolean) {
      if (paramBot.getMethodContext().game.isFixed()) {
        localGraphics2D.setColor(Color.black);
        localGraphics2D.fill(new Rectangle(9, 459, 100, 15));
        localGraphics2D.dispose();
      } else {
        localGraphics2D.setColor(Color.black);
        localGraphics2D.drawRect(8, 555, 100, 15);
        localGraphics2D.dispose();
      }
    }
    return localBufferedImage1;
  }
}